package org.springblade.modules.admin.service.suger.impl;

import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.enums.TaskStatusEnum;
import org.springblade.modules.admin.service.IEstateBuildingService;
import org.springblade.modules.admin.service.suger.IEstateMalfunctionScreenService;
import org.springblade.modules.admin.vo.MaintainPlanContentVO;
import org.springblade.modules.admin.vo.sugar.MalScreenVO;
import org.springblade.modules.admin.vo.sugar.TodayMalDataVO;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author ysq
 * @Date 2022/12/22
 **/
@AllArgsConstructor
@Service
public class EstateMalfunctionScreenServiceImpl implements IEstateMalfunctionScreenService {

	private final IEstateBuildingService estateBuildingService;

	@Override
	public TodayMalDataVO todayData() {
		TodayMalDataVO todayMalDataVO = new TodayMalDataVO();
		List<MaintainPlanContentVO> malElePlanList = estateBuildingService.estateMalElevatorPlanList(AuthUtil.getTenantId(), DateUtil.date(), DateUtil.date());
		// 待开始
		long startNum = malElePlanList.stream().filter(t -> t.getStatus() != null && t.getStatus().equals(TaskStatusEnum.WX_TO_START.getStatus())).map(t -> t.getElevatorId()).distinct().count();
		todayMalDataVO.setStartNum(startNum);

		// 维修中
		long malNum = malElePlanList.stream().filter(t -> t.getStatus() != null && t.getStatus().equals(TaskStatusEnum.WX_UNDER_WAY.getStatus())).map(t -> t.getElevatorId()).distinct().count();
		todayMalDataVO.setMalNum(malNum);

		// 已完成
		long finishNum = malElePlanList.stream().filter(t -> t.getStatus() != null && t.getStatus().equals(TaskStatusEnum.WX_SIGN_FINISH.getStatus())).map(t -> t.getElevatorId()).distinct().count();
		todayMalDataVO.setFinishNum(finishNum);

		return todayMalDataVO;
	}

	@Override
	public List<MalScreenVO> top() {
		List<MalScreenVO> malScreenTop = new ArrayList<>();
		List<MaintainPlanContentVO> malElePlanList = estateBuildingService.estateMalElevatorPlanList(AuthUtil.getTenantId(), null, null);
		if (Func.isEmpty(malElePlanList)) {
			return malScreenTop;
		}

		// 每个项目对应电梯数
		Map<String, Long> eleTotalMap = malElePlanList.stream().collect(Collectors.groupingBy(t -> t.getBuildingName(),
			Collectors.mapping(t -> t.getEstateElevatorId(), Collectors.collectingAndThen(Collectors.toSet(), t -> t.stream().count()))));
		// 每个项目对应报障数
		Map<String, Long> malTotalMap = malElePlanList.stream().filter(t -> t.getId() != null).collect(Collectors.groupingBy(t -> t.getBuildingName(),
			Collectors.mapping(t -> t.getId(), Collectors.collectingAndThen(Collectors.toSet(), t -> t.stream().count()))));

		eleTotalMap.forEach((k, v) -> {
			MalScreenVO malScreenVO = new MalScreenVO();
			malScreenVO.setName(k);
			malScreenVO.setEleTotal(v);
			malScreenVO.setRepairTotal(Func.isEmpty(malTotalMap.get(k)) ? 0 : malTotalMap.get(k));
			malScreenTop.add(malScreenVO);
		});

		return malScreenTop.stream().sorted(Comparator.comparing(MalScreenVO :: getRepairTotal).reversed()).collect(Collectors.toList());
	}

	@Override
	public List<MalScreenVO> malDetail(Integer mode) {
		List<MalScreenVO> malScreenList = new LinkedList<>();
		Map<String, Long> resultMap = null;
		Date startTime = null;
		Date endTime = DateUtil.date();
		if (mode == 0)  {
			// 近7天
			startTime = DateUtil.offsetDay(endTime, -7);
			List<MaintainPlanContentVO> malElePlanList = estateBuildingService.estateMalElevatorPlanList(AuthUtil.getTenantId(),startTime, endTime);
			resultMap = malElePlanList.stream().filter(t -> t.getId() != null).collect(Collectors.groupingBy(t -> DateUtil.format(t.getCreateTime(), "yyyy/M/d"), Collectors.counting()));
		} else {
			// 近6个月
			startTime = DateUtil.beginOfMonth(DateUtil.offsetMonth(endTime, -6));
			List<MaintainPlanContentVO> malElePlanList = estateBuildingService.estateMalElevatorPlanList(AuthUtil.getTenantId(),startTime, endTime);
			resultMap = malElePlanList.stream().filter(t -> t.getId() != null).collect(Collectors.groupingBy(t -> DateUtil.format(t.getCreateTime(), "yyyy年M月"), Collectors.counting()));
		}

		// 封装数据
		if (mode == 0) {
			for (int i = 6; i >=0 ; i--) {
				Date itemDate = DateUtil.offsetDay(endTime, -i);
				String dateKey = DateUtil.format(itemDate, "yyyy/M/d");
				MalScreenVO malScreenVO = new MalScreenVO();
				malScreenVO.setName(dateKey.substring(5));
				if (resultMap.containsKey(dateKey)) {
					malScreenVO.setRepairTotal(resultMap.get(dateKey));
				} else {
					malScreenVO.setRepairTotal(0L);
				}
				malScreenList.add(malScreenVO);
			}
		} else {
			for (int i = 5; i >=0 ; i--) {
				Date itemDate = DateUtil.offsetMonth(endTime, -i);
				String dateKey = DateUtil.format(itemDate, "yyyy年M月");
				MalScreenVO malScreenVO = new MalScreenVO();
				malScreenVO.setName(dateKey.substring(5));
				if (resultMap.containsKey(dateKey)) {
					malScreenVO.setRepairTotal(resultMap.get(dateKey));
				} else {
					malScreenVO.setRepairTotal(0L);
				}
				malScreenList.add(malScreenVO);
			}
		}

		return malScreenList;
	}
}
